package com.newshub.business.service.impl;

import com.newshub.business.entity.Users;
import com.newshub.business.dto.UserQueryDTO;
import com.newshub.business.mapper.UsersMapper;
import com.newshub.business.service.UsersService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 用户服务实现类
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据ID获取用户")
    public Users getById(Integer id) {
        return usersMapper.selectById(id);
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据用户名获取用户")
    public Users getByUsername(String username) {
        return usersMapper.selectByUsername(username);
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "根据邮箱获取用户")
    public Users getByEmail(String email) {
        return usersMapper.selectByEmail(email);
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "获取所有用户")
    public List<Users> getAll() {
        return usersMapper.selectAllUsers();
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "创建用户")
    public boolean create(Users user) {
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        return usersMapper.insert(user) > 0;
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "更新用户")
    public boolean update(Users user) {
        user.setUpdatedAt(LocalDateTime.now());
        return usersMapper.updateById(user) > 0;
    }

    @Override
    @BusinessLog(level = LogLevel.WARN, description = "删除用户")
    public boolean delete(Integer id) {
        return usersMapper.deleteById(id) > 0;
    }

    @Override
    @BusinessLog(level = LogLevel.INFO, description = "更新用户登录信息")
    public boolean updateLoginInfo(Integer id, LocalDateTime lastLoginTime) {
        return usersMapper.updateLoginInfo(id, lastLoginTime) > 0;
    }

    @Override
    public boolean existsByUsername(String username) {
        return usersMapper.selectByUsername(username) != null;
    }

    @Override
    public boolean existsByEmail(String email) {
        return usersMapper.selectByEmail(email) != null;
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "分页查询用户")
    public List<Users> getUsersByPage(UserQueryDTO queryDTO, int offset, int limit) {
        return usersMapper.selectByPage(queryDTO, offset, limit);
    }
    
    @Override
    public long countUsersByPage(UserQueryDTO queryDTO) {
        return usersMapper.countByPage(queryDTO);
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "更新用户状态")
    public boolean updateStatus(Integer id, Integer status) {
        return usersMapper.updateStatus(id, status) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.WARN, description = "批量删除用户")
    public boolean deleteBatchUsers(List<Integer> ids) {
        return usersMapper.deleteBatchByIds(ids) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "批量更新用户状态")
    public boolean updateBatchStatus(List<Integer> ids, Integer status) {
        return usersMapper.updateBatchStatus(ids, status) > 0;
    }
    
    @Override
    @BusinessLog(level = LogLevel.INFO, description = "获取用户统计信息")
    public Map<String, Object> getUserStats() {
        Map<String, Object> stats = new HashMap<>();
        
        // 总用户数
        long totalUsers = usersMapper.countAllUsers();
        stats.put("total", totalUsers);
        
        // 管理员数量
        long adminCount = usersMapper.countByRole("admin");
        stats.put("admin", adminCount);
        
        // 编辑数量
        long editorCount = usersMapper.countByRole("user");
        stats.put("editor", editorCount);
        
        // 已禁用数量
        long disabledCount = usersMapper.countByStatus(0);
        stats.put("disabled", disabledCount);
        
        return stats;
    }
}



